package src;

import ca.uqac.dim.turtledb.*;
import ca.uqac.dim.turtledb.physical.Fragment;
import ca.uqac.dim.turtledb.physical.Network;
import ca.uqac.dim.turtledb.physical.Site;
import ca.uqac.dim.turtledb.querytranslator.QueryTranslator;
import ca.uqac.dim.turtledb.util.FileReadWrite;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * User: damien Date: 12-11-24 Time: 15:20 Basic Demo Do nothing but contains a
 * Main usefull for current dev
 */
public class Exemple {

    private static Table r_Astronaut;
    private static Table r_Mission;
    private static Table r_Crew;
    private static Table r_Rocket;
    private static Network network;
    private static CentralizedCommunicator centralizedCommunicator;
    //public static final String SQL_QUERY = "Select Astronaut, Mission, Group From Astronaut, Crew Where Astronaut.Name = Crew.Astronaut";
    public static final String SQL_QUERY = "Select Astronaut, Role, Name From Mission, Crew Where Crew.Mission = Mission.Name";

    public static void main(String[] args) throws Exception {
        //QueryTranslator.test();

        try {
            getTables();

            //create Network
            createNetwork();

            // Populate a communicator with sites and tables and start it
            centralizedCommunicator = network.createCommunicator();
            centralizedCommunicator.run();

            Relation query = QueryTranslator.translate(SQL_QUERY);

            QueryOptimizer queryOptimizer = network.getQueryOptimizer();
            QueryPlan optimizedQueryPlan = queryOptimizer.optimizeQuery(query);

            int cost = 0;
            cost = queryOptimizer.getCost(optimizedQueryPlan);

            System.out.println("QueryOptimizer say : made an optimizedQueryPlan cost " + cost);
            System.out.println("QueryOptimizer say : Amazing !");

            QueryProcessor queryProcessor = centralizedCommunicator.getQueryProcessor(optimizedQueryPlan);
            queryProcessor.run();
            Relation result = queryProcessor.getResult();

            System.out.println(result);

        } catch (Exception e) {
            System.out.println("Exception qui dit : " + e.getMessage());
            e.printStackTrace();
        }

        System.exit(1);

    }

    private static void getTables() {
        try {
            r_Astronaut = (Table) XmlQueryParser.parse(FileReadWrite.getFileContents("D:/Mes documents/Documents/NetBeansProjects/EDMADA/trunk/Source/data/Space/Astronaut.xml"));
            r_Astronaut.setName("Astronaut");
            r_Mission = (Table) XmlQueryParser.parse(FileReadWrite.getFileContents("D:/Mes documents/Documents/NetBeansProjects/EDMADA/trunk/Source/data/Space/Mission.xml"));
            r_Mission.setName("Mission");
            r_Crew = (Table) XmlQueryParser.parse(FileReadWrite.getFileContents("D:/Mes documents/Documents/NetBeansProjects/EDMADA/trunk/Source/data/Space/Crew.xml"));
            r_Crew.setName("Crew");
            r_Rocket = (Table) XmlQueryParser.parse(FileReadWrite.getFileContents("D:/Mes documents/Documents/NetBeansProjects/EDMADA/trunk/Source/data/Space/Rocket.xml"));
            r_Rocket.setName("Rocket");
        } catch (FileNotFoundException e) {
            System.err.println("File not found");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Error reading files");
            System.exit(1);
        } catch (XmlQueryParser.ParseException e) {
            System.err.println("Error parsing XML files");
            System.exit(1);
        }
        if (r_Astronaut == null || r_Mission == null || r_Crew == null || r_Rocket == null) {
            System.err.println("Error reading Space database");
            System.exit(1);
        }
    }

    private static Network createNetwork() {
        /*
         * Le Site 1 heberge le centre de recrutement et de gestion des
         * ressources humaines Il forme les astronautes et constitue les équipes
         * Il dispose donc des données concernant les astronautes et les équipes
         */
        Site site_1 = new Site("Departement RH", 42);
        site_1.addFragment(new Fragment(r_Astronaut, (Condition) null));
        site_1.addFragment(new Fragment(r_Crew, (Condition) null));
        /*
         * Le site deux s'occuper de lancer les missions, Ca principale
         * préocupation est la logistique entourant l'engin à lancer. Il dispose
         * donc des données concernant les engins et les missions
         */
        Site site_2 = new Site("Departement Mission", 69);
        site_2.addFragment(new Fragment(r_Mission, (Condition) null));
        site_2.addFragment(new Fragment(r_Rocket, (Condition) null));

        // Faire un network
        network = new Network();
        // Mapper dans le network
        network.link(site_1, site_2, 30);

        return network;
    }
}
